package com.estar.assistsurvy.activity;

import java.util.ArrayList;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.WalletListviewAdapter;
import com.estar.assistsurvy.entity.RequestWalletChange;
import com.estar.assistsurvy.entity.ResponseWalletChange;
import com.estar.assistsurvy.entity.WalletItem;
import com.estar.assistsurvy.thread.WalletChangeThread;
import com.estar.assistsurvy.utils.ExitUtils;
import com.estar.assistsurvy.utils.MightypleUtil;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class WalletActivity extends BaseActivity implements OnClickListener,OnItemClickListener{

	private ListView accountList;
	private WalletListviewAdapter adapter;
	private LinearLayout returnBack;
	private Intent intent;
	private SharedPreferences prefer;
	private String uuid,deviceId;
	private ResponseWalletChange wallet;
	private ProgressDialog dialog;
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(dialog!=null){
				dialog.dismiss();
			}
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;
				
			case 0:
				wallet=new ResponseWalletChange();
				wallet=(ResponseWalletChange) msg.obj;
				if(wallet.getCode().equals("1")){
					adapter=new WalletListviewAdapter(setData(), WalletActivity.this);
					accountList.setAdapter(adapter);
				}else if(wallet.getCode().equals("0")){
					showShortToast(wallet.getMessages());
				}else if(wallet.getCode().equals("2")){
					ExitUtils.Unlogin(WalletActivity.this, wallet.getMessages());

				}
				break;

			default:
				break;
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wallet);
		initView();
	}
	
	private void initView(){
		accountList=(ListView) findViewById(R.id.accountList);
		accountList.setOnItemClickListener(this);
		returnBack=find(R.id.returnBack);
		returnBack.setOnClickListener(this);
		
		prefer=getSharedPreferences("user", MODE_PRIVATE);
		uuid=prefer.getString("uuid", "");
		deviceId=MightypleUtil.getDeviceId(this);
		RequestWalletChange walletChange=new RequestWalletChange(deviceId,uuid);
		new WalletChangeThread(this, handler, walletChange).start();;
		dialog=ProgressDialog.show(this, null, "获取钱包信息…", false, false);
	}
	
	
	private ArrayList<WalletItem> setData(){
		ArrayList<WalletItem> list=new ArrayList<WalletItem>();
		WalletItem item=new WalletItem(R.drawable.icon_money, "查勘费", "余额:"+wallet.getTotalAmount()+"元");
		list.add(item);
		item=new WalletItem(R.drawable.icon_card, "银行卡", "");
		list.add(item);
		return list;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBack:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		switch (position) {
		case 0:
			intent=new Intent(WalletActivity.this, BalanceActivity.class);
			intent.putExtra("amount", wallet.getTotalAmount());
			startActivity(intent);
			break;
		case 1:
			intent=new Intent(WalletActivity.this, ShowBankCardActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}
}
